﻿using System; 
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Matrix.Common.DBUtility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.DAL
{
	public partial class McMessageRecipientsDAL : BaseDAL<McMessageRecipientsDAL>
	{
		#region 默认方法
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(McMessageRecipients model)
		{
			var strSql=new StringBuilder();
			strSql.Append("insert into McMessageRecipients(");			
            strSql.Append("MsgId,ReceiverId,MsgCate,MsgType,IsRead,Created,Updated,CreatedBy,UpdatedBy,Disabled,Status,IsDelete");
			strSql.Append(") values (");
            strSql.Append("@MsgId,@ReceiverId,@MsgCate,@MsgType,@IsRead,@Created,@Updated,@CreatedBy,@UpdatedBy,@Disabled,@Status,@IsDelete");            
            strSql.Append(") ");            
            strSql.Append(";select @@IDENTITY");		
			SqlParameter[] parameters = {
			            new SqlParameter("@MsgId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ReceiverId", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgCate", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgType", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsRead", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Updated", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@Disabled", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
			            
            parameters[0].Value = model.MsgId;                        
            parameters[1].Value = model.ReceiverId;                        
            parameters[2].Value = model.MsgCate;                        
            parameters[3].Value = model.MsgType;                        
            parameters[4].Value = model.IsRead;                        
            parameters[5].Value = model.Created;                        
            parameters[6].Value = model.Updated;                        
            parameters[7].Value = model.CreatedBy;                        
            parameters[8].Value = model.UpdatedBy;                        
            parameters[9].Value = model.Disabled;                        
            parameters[10].Value = model.Status;                        
            parameters[11].Value = model.IsDelete;            			var obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);			
			return obj == null ? 0 : Convert.ToInt32(obj);
		}
				
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(McMessageRecipients model)
		{
			var strSql=new StringBuilder();
			strSql.Append("update McMessageRecipients set ");
			                                                
            strSql.Append(" MsgId = @MsgId , ");                                    
            strSql.Append(" ReceiverId = @ReceiverId , ");                                    
            strSql.Append(" MsgCate = @MsgCate , ");                                    
            strSql.Append(" MsgType = @MsgType , ");                                    
            strSql.Append(" IsRead = @IsRead , ");                                    
            strSql.Append(" Created = @Created , ");                                    
            strSql.Append(" Updated = @Updated , ");                                    
            strSql.Append(" CreatedBy = @CreatedBy , ");                                    
            strSql.Append(" UpdatedBy = @UpdatedBy , ");                                    
            strSql.Append(" Disabled = @Disabled , ");                                    
            strSql.Append(" Status = @Status , ");                                    
            strSql.Append(" IsDelete = @IsDelete  ");            			
			strSql.Append(" where Id=@Id ");
						
			SqlParameter[] parameters = {
			            new SqlParameter("@Id", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgId", SqlDbType.Int,4) ,            
                        new SqlParameter("@ReceiverId", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgCate", SqlDbType.Int,4) ,            
                        new SqlParameter("@MsgType", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsRead", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Created", SqlDbType.DateTime) ,            
                        new SqlParameter("@Updated", SqlDbType.DateTime) ,            
                        new SqlParameter("@CreatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdatedBy", SqlDbType.Int,4) ,            
                        new SqlParameter("@Disabled", SqlDbType.Bit,1) ,            
                        new SqlParameter("@Status", SqlDbType.Int,4) ,            
                        new SqlParameter("@IsDelete", SqlDbType.Bit,1)             
              
            };
						            
            parameters[0].Value = model.Id;                        
            parameters[1].Value = model.MsgId;                        
            parameters[2].Value = model.ReceiverId;                        
            parameters[3].Value = model.MsgCate;                        
            parameters[4].Value = model.MsgType;                        
            parameters[5].Value = model.IsRead;                        
            parameters[6].Value = model.Created;                        
            parameters[7].Value = model.Updated;                        
            parameters[8].Value = model.CreatedBy;                        
            parameters[9].Value = model.UpdatedBy;                        
            parameters[10].Value = model.Disabled;                        
            parameters[11].Value = model.Status;                        
            parameters[12].Value = model.IsDelete;                        return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
				
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("update McMessageRecipients set IsDelete=1 ");
			strSql.Append(" where Id=@Id");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters) > 0;
		}
		
				/// <summary>
		/// 批量删除一批数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			var strSql=new StringBuilder();
			strSql.Append("update McMessageRecipients set IsDelete=1 ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
		}
						
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public DataSet GetModel(int Id)
		{
			var strSql=new StringBuilder();
			strSql.Append("select Id, MsgId, ReceiverId, MsgCate, MsgType, IsRead, Created, Updated, CreatedBy, UpdatedBy, Disabled, Status, IsDelete  ");			
			strSql.Append("  from McMessageRecipients ");
			strSql.Append(" where Id=@Id  and IsDelete=0");
						SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;
			
			var ds = DbHelperSQL.Query(strSql.ToString(),parameters);			
			return ds.Tables[0].Rows.Count>0 ? ds : null;
		}
				
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			var strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM McMessageRecipients where IsDelete=0 ");
			if(!string.IsNullOrEmpty(strWhere))
			{
				strSql.Append(" and "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}		
		#endregion
		
		#region 扩展方法

	    /// <summary>
	    /// 获取分页数据
	    /// </summary>
	    /// <param name="pageIndex"></param>
	    /// <param name="pageSize"></param>
	    /// <param name="shopId"></param>
	    /// <param name="searchKeyWord"></param>
	    /// <param name="colOrder"></param>
	    /// <param name="totalCount"></param>
	    /// <returns></returns>
	    public List<ViewMcMessageReceivedDto> GetPagingListForShopGroup(int pageIndex, int pageSize, int shopId, string searchKeyWord, string colOrder, ref int totalCount)
	    {
	        if (string.IsNullOrEmpty(colOrder)) colOrder = " Created Desc ";

            string strWhere = " ReceiverId = " + shopId;

            if (!string.IsNullOrEmpty(searchKeyWord))
            {
                strWhere += " AND (Title LIKE '%" + searchKeyWord + "%' OR Body LIKE '%" + searchKeyWord + "%') ";
            }

            const string tables = "(SELECT mr.Id, m.Title, m.Body, m.SendToAll, m.SendToGroup, m.SendToPrivate, " +
                                  "mr.ReceiverId, mr.IsRead, mr.Created, m.OrderId " +
                                  "FROM McMessages m INNER JOIN dbo.McMessageRecipients mr ON m.Id = mr.MsgId " +
                                  "WHERE m.IsDelete = 0 AND mr.IsDelete = 0 ) n ";

            return GetSimpleListPage<ViewMcMessageReceivedDto>(pageIndex, pageSize, tables, null, null, colOrder, strWhere, ref totalCount);
        }

        /// <summary>
        /// 全部设置为已读
        /// </summary>
        /// <returns></returns>
	    public bool UpdateAlllRead()
	    {
            var strSql = new StringBuilder();
            strSql.Append("update McMessageRecipients set IsDelete=1 ");
            strSql.Append(" where IsDelete=0  ");
            return DbHelperSQL.ExecuteSql(strSql.ToString()) > 0;
	    }

		#endregion
	}
}
